package com.student.education.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.student.education.common.CommonPage;
import com.student.education.dao.UserDao;
import com.student.education.entity.CheckWork;
import com.student.education.dao.CheckWorkDao;
import com.student.education.service.CheckWorkService;
import com.student.education.to.CheckWorkQueryTo;
import com.student.education.to.CheckWorkTo;
import com.student.education.to.UserTo;
import com.student.education.vo.CheckWorkVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 考勤表(CheckWork)表服务实现类
 *
 * @author makejava
 * @since 2021-09-25 16:18:36
 */
@Service("checkWorkService")
public class CheckWorkServiceImpl implements CheckWorkService {
    @Resource
    private CheckWorkDao checkWorkDao;
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CheckWork queryById(Integer id) {
        return this.checkWorkDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param checkWork 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(List<CheckWorkTo> checkWork) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        try {
            for (int i=0;i<checkWork.size();i++){
                String month = checkWork.get(i).getMonth();
                Date dateStart = dateFormat.parse(month);
                checkWork.get(i).setMonth(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateStart));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return checkWorkDao.insert(checkWork);
    }

    /**
     * 修改数据
     *
     * @param checkWork 实例对象
     * @return 实例对象
     */
    @Override
    public CheckWork update(CheckWork checkWork) {
        this.checkWorkDao.update(checkWork);
        return this.queryById(checkWork.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.checkWorkDao.deleteById(id) > 0;
    }

    @Override
    public CommonPage<CheckWorkQueryTo> queryAll(CheckWorkVo vo) {
        UserTo userTo = userDao.queryById(vo.getUserId());
        List<CheckWorkQueryTo> list = new ArrayList<>();
        if (Objects.equals(userTo.getRoleId(),"3")){
            if (vo.getPageUtil() != null) {
                PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
            }
            list = checkWorkDao.queryAllByStudent(vo,userTo.getClassId());
        }else {
            if (vo.getPageUtil() != null) {
                PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
            }
            list = checkWorkDao.queryAll(vo,userTo.getClassId());
        }

        return CommonPage.restPage(list);
    }
}
